//
//  XLGCheckNewVersion.m
//  etionCBLogic
//
//  Created by chen on 13-8-29.
//  Copyright (c) 2013年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGCheckNewVersion.h"

#import "XLGCheckNewVersionInternal.h"

NSString *const XLGCheckNewVersionXLGCheckNewVersionResultNotification = @"XLGCheckNewVersionXLGCheckNewVersionResultNotification";

NSString *const XLGCheckNewVersionXLGCheckNewVersionResultNewKey = @"XLGCheckNewVersionXLGCheckNewVersionResultNewKey";

static XLGCheckNewVersion *s_checkVersion;

@interface XLGCheckNewVersion ()
{
    XLGCheckNewVersionInternal *_internal;
}

@property(nonatomic,retain,readonly) XLGCheckNewVersionInternal *internal;

@end


@implementation XLGCheckNewVersion

@synthesize internal = _internal;

-(id)init
{
    self = [super init];
    
    _internal = [XLGCheckNewVersionInternal new];
    
    return self;
}

+(XLGCheckNewVersion*)checkNewVersionInstance
{
    if(s_checkVersion == nil)
        s_checkVersion = [XLGCheckNewVersion new];
    return s_checkVersion;
}

+ (void)_checkNewVersionCompletion:(void(^)(NSInteger nResult,XLGUpdateInfoObj *updateInfo))completion
{
    [[XLGCheckNewVersion checkNewVersionInstance].internal stopRequest];
    
    [[XLGCheckNewVersion checkNewVersionInstance].internal requestAsyncBlock:^
    {
        XLGResult* r = [[XLGCheckNewVersion checkNewVersionInstance].internal checkVersion];
        
        switch (r.outResult)
        {
            case XLGCheckNewVersionForceUpdateCode:
            case XLGCheckNewVersionSuggestUpdateCode:
            {
                [self saveUpdateInfo:(XLGUpdateInfoObj*)r.outObj];
                completion(r.outResult,(XLGUpdateInfoObj*)r.outObj);
                break;
            }
            case TRUE:
            {
                [self saveUpdateInfo:nil];
                completion(r.outResult,(XLGUpdateInfoObj*)r.outObj);
                break;
            }
            case NETWORKOFFLINE:
            {
                completion(r.outResult,nil);
                break;
            }
            case TERMINATEREADWRITE:
            {
                completion(r.outResult,nil);
                break;
            }
            case PROTOCOLRETOBJNIL:
            {
                completion(r.outResult,nil);
                break;
            }
            default:
            {
                completion(r.outResult,nil);
                break;
            }
        }
    }];
}

+ (void)checkNewVersion
{
    DispatchGlobalThreadAsync(^
    {
        [XLGCheckNewVersion _checkNewVersionCompletion:^(NSInteger nResult, XLGUpdateInfoObj *updateInfo)
        {
            switch (nResult)
            {
                case XLGCheckNewVersionForceUpdateCode:
                case XLGCheckNewVersionSuggestUpdateCode:
                {
                    [self notificationNewVersionTag:YES];
                    break;
                }
                case TRUE:
                {
                    [self notificationNewVersionTag:NO];
                    break;
                }
            }
        }];
    });
}

+ (XLGResult*)checkNewVersionAttachDOM:(XMSDataOperationManager *)attachdom
{
    XLGCheckNewVersionInternal *internal = [[[XLGCheckNewVersionInternal alloc]initWithDataOperationManagerDelegate:nil AttachDOM:attachdom] autorelease_mm];
    if(internal != nil)
    {
        XLGResult* r = [internal checkVersion];
        
        switch (r.outResult)
        {
            case XLGCheckNewVersionForceUpdateCode:
            case XLGCheckNewVersionSuggestUpdateCode:
            {
                [self saveUpdateInfo:(XLGUpdateInfoObj*)r.outObj];
                break;
            }
            case TRUE:
            {
                [self saveUpdateInfo:nil];
                break;
            }
        }
        
        [attachdom detachDataOperationManager:@[internal]];
        
        return r;
    }
    return [XLGResult resultWithOutResult:TERMINATEREADWRITE];
}

+ (void)saveUpdateInfo:(XLGUpdateInfoObj*)updateInfo
{
    NSMutableArray *tip = [NSMutableArray array];
    NSString *newVersionName = @"";
    NSString *http = @"";
    
    BOOL remind = [[XLGContext getDocumentSysCfgDictionary][XLGContextUpdateInfo][XLGContextUpdateInfoRemind] boolValue];
    
    if(updateInfo != nil)
    {
        [XLGContext context].hadNewVsersion = YES;
        [XLGContext context].forceUpdate = updateInfo.enforceUpdate;
        http = updateInfo.updateUrl;
        newVersionName = updateInfo.versionName;
        [tip addObjectsFromArray:updateInfo.updateInfo];
    }
    else
    {
        [XLGContext context].hadNewVsersion = NO;
        [XLGContext context].forceUpdate = NO;
    }
    
    NSDictionary *dic = @{XLGContextUpdateInfoNewTag:@([XLGContext context].hadNewVsersion),XLGContextUpdateInfoForceUpdate:@([XLGContext context].forceUpdate),XLGContextUpdateInfoTip:tip,XLGContextUpdateInfoNewVersionName:newVersionName,XLGContextUpdateInfoRemind:@(remind),XLGContextUpdateInfoNewVersionHttp:http};
    [XLGContext saveSysCfgValue:dic forKey:XLGContextUpdateInfo];
}

+ (void)notificationNewVersionTag:(BOOL)bNewVersion
{
    DispatchMainThreadAsync(^
    {
        [NSNotificationCenter postNotification:XLGCheckNewVersionXLGCheckNewVersionResultNotification userInfo:@{XLGCheckNewVersionXLGCheckNewVersionResultNewKey:@(bNewVersion)}];
    });
}

@end

@implementation XLGCheckNewVersion (CViewBaseViewController)

+ (void)checkNewVersion:(id)progressDelegate completion:(void(^)(NSInteger nResult))completion
{
    DispatchGlobalThreadAsync(^
    {
        [progressDelegate beginProgressView:nil funcationName:@"checkNewVersion"];
        
        [XLGCheckNewVersion _checkNewVersionCompletion:^(NSInteger nResult, XLGUpdateInfoObj *updateInfo)
        {
            DispatchMainThreadAsync(^
            {
                completion(nResult);
            });
        }];
        
        [progressDelegate endProgressView:nil funcationName:@"checkNewVersion"];
    });
}

@end